<?php

class AdminMapper extends SON_Db_DataMapperAbstract
{
    protected $_dbTable = "DbTable_Admin";
    protected $_model = "Admin";

    protected function _insert(SON_Db_DomainObjectAbstract $obj)
    {
        try {
            $dbTable = $this->getDbTable();
            $data = array(
                'nome' => $obj->getNome(),
                'email' => $obj->getEmail(),
                'senha' => $obj->getSenha()
            );
            return $dbTable->insert($data);
        } catch (Zend_Exception $e) {
            throw new Exception($e);
        }
    }

    protected function _update(SON_Db_DomainObjectAbstract $obj)
    {
        try {
            $dbTable = $this->getDbTable();
            $data = array(
                'nome' => $obj->getNome(),
                'email' => $obj->getEmail(),
                'senha' => $obj->getSenha()
            );
            $data = array_map(function($value) { return (!is_null($value) && empty($value)) ? new Zend_Db_Expr('NULL') : $value; }, $data);
            $dbTable->update(array_filter($data), array('id = ?' => $obj->getId()));
            return true;
        } catch (Zend_Exception $e) {
            throw new Exception($e);
        }
    }
}

